package com.eshin.mdcdecorator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.UrlPathHelper;

import com.eshin.DefaultKeywords;

/**
 * Http请求拦截器，其主要功能是：
 * <p>
 * 1. 识别请求报文
 * <p>
 * 2. 解析报文关键字
 * <p>
 * 3. 将值填入到MDC中
 *
 * @author wuhang
 */
public class MDCSpringMvcHandlerInterceptor extends HandlerInterceptorAdapter {
    
    
    @Autowired
    private DefaultKeywords defaultKeywords;
    
    @Autowired
    MDCSpringMvcHandlerInterceptor proxyThis;
    
    @Override
    public boolean preHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        
        MDC.setContextMap(defaultKeywords.getDefaultKeyValues());
        
        return true;
    }
    
    @Override
    public void afterCompletion(
            HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        MDC.clear();
    }
}
